#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cassert>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef long long LL;
#define Rep(i,n) for(int i=0;i<(n);++i) 
#define For(i,a,b) for(int i=(a);i<=(b);++i) 
#define ForI(TP,i,coll) for(TP (i)=coll.begin();(i)!=coll.end();(i)++)
#define ForD(i,a,b) for(int i=(a);i>=(b);--i)
#define All(a) a.begin(), a.end()

struct program
{
	VS subs;
	int sumsubs;
	int cntSubs(){return subs.size();}
	void init(string spro)
	{
		subs.resize(0); //

		string sub;
		for(int i=1;i<=spro.size();i++)
		{
			if(spro[i]=='$' || i==spro.size())
			{
				subs.push_back(sub);
				sub.clear();
			}
			else
				sub+=spro[i];
		}

		sumsubs=0;
		Rep(i, subs.size()) sumsubs+=subs[i].size();
	}
} prog;
string _input;

struct len
{
	vector<LL> buf;
	void init(int n)
	{
		buf.resize(n+1, -1);
	}
	LL operator()(int n)
	{
		LL& ret=buf[n];
		if(ret==-1)
		{
			if(n==0)
				ret=_input.size();
			else
			{
				LL lnn=(*this)(n-1);
				if(lnn>1000000100)
					ret=lnn;
				else
					ret=lnn*prog.cntSubs() + prog.sumsubs;
			}
		}
		return ret;
	}
} l;

char get(int n, LL i)
{
	if(!(i<l(n))) return '-';
	if(n==0) return _input[i];

	For(j, 0, prog.cntSubs())
	{
		if(i<l(n-1))
			return get(n-1, i);
		i-=l(n-1);
		if(i<prog.subs[j].size())
			return prog.subs[j][i];
		i-=prog.subs[j].size();
	}
	return '-';
}


struct EndlessstringMachine
{
	string getFragment(string input, string spro, int s, int min, int max)
	{
		prog.init(spro);
		_input.swap(input);
		l.init(s);

		string ret;
		For(i, min-1, max-1)
			ret+=get(s, i);
		return ret;
	}
};

int main()
{
	{
		EndlessstringMachine em;
		string ret=em.getFragment("xy","$a$bb",100000000,5000,5099);
		cout<< ret <<endl; //americamericamericamericamericameri
		assert(ret=="xybbbbaxyaxybbaxyaxybbbbbbbbaxyaxybbaxyaxybbbbaxyaxybbaxyaxybbbbbbaxyaxybbaxyaxybbbbaxyaxybbaxyaxybb");
	}
	{
		EndlessstringMachine em;
		string ret=em.getFragment("a","$meric$",6,1,35);
		cout<< ret <<endl; //americamericamericamericamericameri
		assert(ret=="americamericamericamericamericameri");
	}
	{
		EndlessstringMachine em;
		string ret=em.getFragment("a","$meric$",6,1,35);
		cout<< ret <<endl; //americamericamericamericamericameri
		assert(ret=="americamericamericamericamericameri");
	}
}